package com.sdry.web.controller.lz;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.llm.BarCode;
import com.sdry.model.lz.Car;
import com.sdry.model.lz.Contacts;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.lz.Materiel;
import com.sdry.model.lz.OutStockOrder;
import com.sdry.model.lz.OutStockOrderDetail;
import com.sdry.model.lz.OutStockOrderDetailPDAEntity;
import com.sdry.model.lz.OutStockOrderPDAEntity;
import com.sdry.model.lz.Staff;
import com.sdry.model.lz.Warehouse;
import com.sdry.model.lz.WarehouseRegion;
import com.sdry.model.zc.ZcGeneralQueryEntity;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.llm.CancellingStockDetailService;
import com.sdry.service.lz.StockOutOrderService;
import com.sdry.service.zc.ZcInventoryManagementService;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName StockOutController
 * @Description 出库管理
 * @Author lz
 * @Date 2019年3月5日 13:58:34
 * @Version 1.0
 */
@Controller
@RequestMapping("/stockOutOrder")
public class StockOutOrderController {
	public static final Integer AFFECT_ROW = 0;
	public static final String STOCKOUTORDER_TITLE = "出库单信息";

	@Resource
	StockOutOrderService stockOutService;

	@Resource
	ZcInventoryManagementService zcInventoryManagementService;
	
	@Resource
	CancellingStockDetailService cancellingStockDetailService;

	/**
	 * 新增出库单
	 * @param response
	 * @param order 出库单
	 */
	@RequestMapping("/addStockOutOrder")
	public void addStockOutOrder(HttpServletResponse response, HttpSession session, OutStockOrder order){
		order.setStatus(0);
		Long affectRow = stockOutService.addStockOutOrder(order);
		try {
			ResponseUtil.write(response, affectRow);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 新增出库单详细
	 * @param response
	 * @param mIdArr 出库单详细
	 * @throws Exception 
	 */
	@RequestMapping("/addStockOutOrderDetail")
	public void addStockOutOrderDetail(OutStockOrderDetail outStockOrderDetail,  HttpServletResponse response, HttpSession session) throws Exception{
		Long affact = stockOutService.addStockOutOrderDetail(outStockOrderDetail);
		ResponseUtil.write(response, affact);
	}
	/*public void addStockOutOrderDetail(HttpServletResponse response, HttpSession session, String firstTableId, @RequestParam("mIdArr[]") String[] mIdArr){
		Long affectRow = 0L;
		if(mIdArr.length > 0){
			for (int i = 0; i < mIdArr.length; i++) {
				System.out.println(mIdArr[i]);
				OutStockOrderDetail outStockOrderDetail = new OutStockOrderDetail();
				outStockOrderDetail.setOut_stock_num_id(Long.parseLong(firstTableId));
				outStockOrderDetail.setMaterial_id(Long.parseLong(mIdArr[i]));
				affectRow = stockOutService.addStockOutOrderDetail(outStockOrderDetail);
			}
			if (affectRow > AFFECT_ROW) {
				try {
					ResponseUtil.write(response, affectRow);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				try {
					ResponseUtil.write(response, AFFECT_ROW);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	 */
	/**
	 * 根据ID删除出库单
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("/deleteOutStockOrderById")
	public void deleteOutStockOrderById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr){
		if(idArr.length > 0){
			for (int i = 0; i < idArr.length; i++) {
				Long affectRow = stockOutService.deleteOutStockOrderById(Long.parseLong(idArr[i]));
				if (affectRow > AFFECT_ROW) {
					try {
						ResponseUtil.write(response, affectRow);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					try {
						ResponseUtil.write(response, AFFECT_ROW);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 根据出库单详细ID删除出库单详细
	 * @param response
	 * @param idArr 出库单详细ID
	 */
	@RequestMapping("/deleteOutStockOrderDetailById")
	public void deleteOutStockOrderDetailById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr){
		if(idArr.length > 0){
			for (int i = 0; i < idArr.length; i++) {
				Long affectRow = stockOutService.deleteOutStockOrderDetailById(Long.parseLong(idArr[i]));
				if (affectRow > AFFECT_ROW) {
					try {
						ResponseUtil.write(response, affectRow);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					try {
						ResponseUtil.write(response, AFFECT_ROW);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 出库单的编辑
	 * @param response
	 * @param outStockOrder 出库单
	 */
	@RequestMapping("/updateStockOutOrder")
	public void updateStockOutOrder(HttpServletResponse response, OutStockOrder outStockOrder){
		Long affectRow = stockOutService.updateStockOutOrder(outStockOrder);
		if(affectRow > AFFECT_ROW){
			try {
				ResponseUtil.write(response, affectRow);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				ResponseUtil.write(response, "");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 行内编辑订单表详细
	 * @param response
	 * @param id 订单ID
	 * @param fieldName 修改的字段名称
	 * @param fieldValue 修改的字段的值
	 */
	@RequestMapping("/updateOutStockOrderDetailById")
	public void updateOutStockOrderDetailById(HttpServletResponse response, String id, String fieldName, String fieldValue){
		if(id != null) {
			Long affectRow = stockOutService.updateOutStockOrderDetailById(id, fieldName, fieldValue);
			if(affectRow > AFFECT_ROW) {
				try {
					ResponseUtil.write(response, affectRow);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				try {
					ResponseUtil.write(response, "");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 分页条件查询(出库单导入)
	 * @param response
	 * @param criteria
	 */
	@RequestMapping("/queryOutStockOrderCriteria")
	public void queryOutStockOrderCriteria(HttpServletResponse response, LzQueryCriteria criteria){
		String keyword01 = criteria.getKeyword01();
		String keyword02 = criteria.getKeyword02();
		//String keyword03 = criteria.getKeyword03();
		if(keyword01 == null){
			keyword01 = "";
		}
		if(keyword02 == null){
			keyword02 = "";
		}
        /*if(keyword03 == null){
            keyword03 = "";
        }*/
		criteria.setKeyword01(keyword01);
		criteria.setKeyword02(keyword02);
		//criteria.setKeyword03(keyword03);
		List<OutStockOrder> outStockOrderList = stockOutService.queryOutStockOrderCriteria(criteria);
		int count = stockOutService.countOutStockOrderCriteria(criteria);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(outStockOrderList);
		jsonObject.put("code", AFFECT_ROW);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*________安卓接口(出库)start_________*/
	/**
	 * 查询所有的出库单
	 * @param response
	 */
	@RequestMapping("/queryOutStockOrderForPDA")
	public void queryOutStockOrderForPDA(HttpServletResponse response){
		List<OutStockOrderPDAEntity> outStockOrderList = stockOutService.queryOutStockOrderForPDA();
		JSONObject jsonObject = new JSONObject();
		if(outStockOrderList.size() > AFFECT_ROW){
			jsonObject.put("status", "1");
			jsonObject.put("data", outStockOrderList);
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 查询所有出库单详细
	 * @param response
	 */
	@RequestMapping("/queryOutStockOrderDetailByIdForPDA")
	public void queryOutStockOrderDetailByIdForPDA(HttpServletResponse response, String out_stock_num_id){
		List<OutStockOrderDetailPDAEntity> outStockOrderDetailList = stockOutService.queryOutStockOrderDetailByIdForPDA(Long.parseLong(out_stock_num_id));
		JSONObject jsonObject = new JSONObject();
		if(outStockOrderDetailList.size() > AFFECT_ROW){
			jsonObject.put("status", "1");
			jsonObject.put("data", outStockOrderDetailList);
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * app提交出库单的详细修改实际数量和出库状态
	 * @param json app提交的出库单详细实体
	 */
	@RequestMapping("/appSubmitOutStockOrderDetail")
	public void appSubmitOutStockOrderDetail(HttpServletResponse response, String json){
		//json转list集合
		List<OutStockOrderDetailPDAEntity> outStockOrderDetailPDAEntityList = null;
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		outStockOrderDetailPDAEntityList = gson.fromJson(json, new TypeToken<List<OutStockOrderDetailPDAEntity>>() {}.getType());
		if(outStockOrderDetailPDAEntityList.size() > 0) {
			for (OutStockOrderDetailPDAEntity outStockOrderDetail : outStockOrderDetailPDAEntityList) {
				stockOutService.updateOutStockOrderDetailActNum(outStockOrderDetail.getMateriel().getId() + "", outStockOrderDetail.getAct_num() + "");
				stockOutService.updateOutStockOrderStatus(outStockOrderDetail);
			}
			jsonObject.put("status", "1");
			jsonObject.put("data", "出库成功");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	/*________安卓接口(出库)end_________*/
	/**
	 * 根据订单编号查询订单详细
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryOutStockOrderCriteriaById")
	public void queryOutStockOrderCriteriaById(HttpServletResponse response, LzQueryCriteria criteria) throws Exception{
		String keyword01 = criteria.getKeyword01();
		String keyword02 = criteria.getKeyword02();
		if(keyword01 == null){
			keyword01 = "";
		}
		if(keyword02 == null){
			keyword02 = "";
		}
		criteria.setKeyword01(keyword01);
		criteria.setKeyword02(keyword02);
		List<OutStockOrderDetail> outStockOrderList = stockOutService.queryOutStockOrderCriteriaById(criteria);
		int count = stockOutService.countOutStockOrderCriteriaById(criteria);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(outStockOrderList);
		jsonObject.put("code", AFFECT_ROW);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<String> titleListTemplateMachineList(){
		List<String> list = new ArrayList<>();
		list.add("出库单号");
		list.add("业务单号");
		list.add("出库类型");
		list.add("出库模式");
		list.add("状态");
		list.add("客户");
		list.add("收货方");
		list.add("要求出库数量");
		list.add("实际出库数量");
		list.add("要求出库日期");
		list.add("创建人");
		list.add("创建时间");
		return list;
	}

	@RequestMapping("/exportFileMachineList")
	public void ExportFileMachineList(HttpServletResponse response) throws Exception {
		downLoadOutOrderTemplet(STOCKOUTORDER_TITLE, titleListTemplateMachineList(), response);
	}

	/**
	 *
	 * @param title
	 * @param list
	 * @param response
	 * @throws Exception
	 */
	public static void downLoadOutOrderTemplet(String title, List<String> list, HttpServletResponse response) throws Exception{
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet(title);
		sheet.setColumnWidth(0, 256 * 20 + 184);
		sheet.setColumnWidth(1, 256 * 20 + 184);
		sheet.setColumnWidth(2, 256 * 20 + 184);
		sheet.setColumnWidth(3, 256 * 20 + 184);
		sheet.setColumnWidth(4, 256 * 20 + 184);
		sheet.setColumnWidth(5, 256 * 20 + 184);
		sheet.setColumnWidth(6, 256 * 20 + 184);
		sheet.setColumnWidth(7, 256 * 20 + 184);
		sheet.setColumnWidth(8, 256 * 20 + 184);
		sheet.setColumnWidth(9, 256 * 20 + 184);
		sheet.setColumnWidth(10, 256 * 20 + 184);
		sheet.setColumnWidth(11, 256 * 20 + 184);
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 创建一个居中格式
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		HSSFFont headerFont = (HSSFFont) wb.createFont();
		//headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		headerFont.setFontName("微软雅黑");
		headerFont.setFontHeightInPoints((short)11);
		style.setFont(headerFont);
		style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		HSSFCell cell;
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				cell = row.createCell(i);
				cell.setCellValue(list.get(i));
				cell.setCellStyle(style);
			}
		}
		list = null;
		// 第六步，将文件存到指定位置
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
		String nowtime = dateFormat.format(now);

		String fileName = title + nowtime +".xls";
		fileName = new String(fileName.getBytes("GBK"),"iso8859-1");
		try {
			response.reset();
			response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下载的文件名
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			OutputStream output = response.getOutputStream();
			BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
			bufferedOutPut.flush();
			wb.write(bufferedOutPut);
			bufferedOutPut.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查找所有客户
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllCustomer")
	public void queryAllCustomer(HttpServletResponse response) throws Exception{
		List<Customer> customerList = stockOutService.queryAllCustomer();
		JSONArray jsonArray = JSONArray.fromObject(customerList);
		if (customerList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}
	
	/**
	 * 查找所有供应商
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllCustomerByType")
	public void queryAllCustomerByType(HttpServletResponse response) throws Exception{
		List<Customer> customerList = stockOutService.queryAllCustomerByType();
		JSONArray jsonArray = JSONArray.fromObject(customerList);
		if (customerList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}
	
	/**
	 * 查找所有库区
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllRegion")
	public void queryAllRegion(HttpServletResponse response) throws Exception{
		List<WarehouseRegion> warehouseRegionList = stockOutService.queryAllRegion();
		JSONArray jsonArray = JSONArray.fromObject(warehouseRegionList);
		if (warehouseRegionList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**
	 * 查找所有人员
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllStaff")
	public void queryAllStaff(HttpServletResponse response) throws Exception{
		List<Staff> staffList = stockOutService.queryAllStaff();
		JSONArray jsonArray = JSONArray.fromObject(staffList);
		if (staffList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**
	 * 查找所有车辆
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllCar")
	public void queryAllCar(HttpServletResponse response) throws Exception{
		List<Car> carList = stockOutService.queryAllCar();
		JSONArray jsonArray = JSONArray.fromObject(carList);
		if (carList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	@RequestMapping("/queryDriver")
	public void queryDriver(HttpServletResponse response,String id) throws Exception{
		Car car = stockOutService.queryDriver(Long.parseLong(id));
		JSONArray jsonArray = JSONArray.fromObject(car);
		ResponseUtil.write(response, jsonArray);
	}
	
	/**
	 * 查询未完成的出库单
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllOutStockOrder")
	public void queryAllOutStockOrder(HttpServletResponse response) throws Exception{
		List<OutStockOrder> outStockOrderList = stockOutService.queryAllOutStockOrder();
		JSONArray jsonArray = JSONArray.fromObject(outStockOrderList);
		if (outStockOrderList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}


	/**
	 * 根据客户名称查询客户地址
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryCustomerByName")
	public void queryCustomerByName(HttpServletResponse response, String customer_name) throws Exception{
		List<Customer> customerList = stockOutService.queryCustomerByName(customer_name);
		JSONArray jsonArray = JSONArray.fromObject(customerList.get(0));
		if (customerList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**
	 * 出库减数量
	 * @param response
	 * @param materiel_id 物料id
	 * @param res3 仓库库存的最终值
	 */
 	@RequestMapping("/updateZcInventoryInfoById")
	public void updateZcInventoryInfoById(HttpServletResponse response, String materiel_id, String res3){
		Long materielId = Long.parseLong(materiel_id);
		Integer materielQuantity = Integer.parseInt(res3);
		//根据物料ID查良品库的库存
		List<ZcInventoryInfoEntity> list = cancellingStockDetailService.getInventoryByMid(materielId);
		//遍历减库存（1个物料ID多条库存记录，减后为0的，删除该记录）
		for(ZcInventoryInfoEntity info : list){
			System.out.println("物料ID="+materielQuantity+":  库存数量="+info.getmNum());
			materielQuantity = materielQuantity - info.getmNum();
			//如果物料数量大于库存
			if(materielQuantity > 0){
				//删除这条数据
				cancellingStockDetailService.deleteInventoryById(info.getId());
			}else{
				//修改库存数量
				Map<String, Object> map = new HashMap<>();
				map.put("id", info.getId());
				map.put("fieldName", "mNum");
				map.put("fieldValue", Math.abs(materielQuantity));
				cancellingStockDetailService.updateInventory(map);
				break;
			}
		}
	}
	/*ZcInventoryInfoEntity zcInventoryInfoEntity = stockOutService.triggerWarning(zcInventoryInfoId);
	if(zcInventoryInfoEntity.getFloor() > Integer.parseInt(finalFieldValue)) {
		PushExample.testSendPush("出库时库存不足！","库存不足","1","2");
	}
	Long affectRow = stockOutService.updateZcInventoryInfoById(materiel_id,  res3);
	if(affectRow > AFFECT_ROW){
		try {
			ResponseUtil.write(response, affectRow);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} else {
		try {
			ResponseUtil.write(response, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * 查询未出库的出库单(Android)
	 * @param response
	 */
 	@RequestMapping("/queryOutStockOrderAndroid")
	public void queryOutStockOrderAndroid(HttpServletResponse response) {
		List<OutStockOrder> outStockOrderList = stockOutService.queryOutStockOrderAndroid();
		JSONObject jsonObject = new JSONObject();
		if(outStockOrderList.size() > AFFECT_ROW){
			jsonObject.put("status", "1");
			jsonObject.put("data", outStockOrderList);
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 根据出库单id查询详细(Android)
	 * @param response
	 */
	@RequestMapping("/queryOutStockOrderDetailByIdAndroid")
	public void queryOutStockOrderDetailByIdAndroid(HttpServletResponse response, String order_id) {
		List<OutStockOrderDetail> outStockOrderDetailList = stockOutService.queryOutStockOrderDetailByIdAndroid(order_id);
		JSONObject jsonObject = new JSONObject();
		if(outStockOrderDetailList.size() > AFFECT_ROW){
			jsonObject.put("status", "1");
			jsonObject.put("data", outStockOrderDetailList);
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 修改出库单状态(Android)
	 * @param response
	 * @param json 接收安卓参数
	 * @throws Exception
	 */
	@RequestMapping("/updateStockOutOrderDetailStatusAndroid")
	public void updateStockOutOrderDetailStatusAndroid(HttpServletResponse response, String json) {
		Gson gson = new Gson();
		List<BarCode> barCodeList = gson.fromJson(json, new TypeToken<List<BarCode>>() {}.getType());
		JSONObject jsonObject = new JSONObject();
		if(barCodeList.size() > AFFECT_ROW) {
			for (int i = 0; i < barCodeList.size(); i++) {
				Long affectRow = stockOutService.updateStockOutOrderDetailStatusAndroid(barCodeList.get(i));
				if(affectRow > AFFECT_ROW){
					try {
						jsonObject.put("status", "1");
						jsonObject.put("data", "提交成功");
						ResponseUtil.write(response, jsonObject);
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					try {
						jsonObject.put("status", "0");
						jsonObject.put("message", "提交失败");
						ResponseUtil.write(response, jsonObject);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} else {
			System.out.println("盘点单详细为空");
		}
	}

	/**根据仓库id查询仓库
	 * @param response
	 * @param id 仓库id
	 * @throws Exception
	 */
	@RequestMapping("/queryWarehouseById")
	public void queryWarehouseById(HttpServletResponse response, String id) throws Exception{
		Warehouse warehouse = stockOutService.queryWarehouseById(Long.parseLong(id));
		JSONArray jsonArray = JSONArray.fromObject(warehouse);
		if (warehouse != null) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**根据用户id查询用户
	 * @param response
	 * @param id 用户id
	 * @throws Exception
	 */
	@RequestMapping("/queryUserById")
	public void queryUserById(HttpServletResponse response, String id) throws Exception{
		ZcSysUserEntity user = stockOutService.queryUserById(Long.parseLong(id));
		JSONArray jsonArray = JSONArray.fromObject(user);
		if (user != null) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**根据用户id查询用户
	 * @param response
	 * @param id 用户id
	 * @throws Exception
	 */
	@RequestMapping("/queryCustomerById")
	public void queryCustomerById(HttpServletResponse response, String id) throws Exception{
		Customer customer = stockOutService.queryCustomerById(Long.parseLong(id));
		JSONArray jsonArray = JSONArray.fromObject(customer);
		if (customer != null) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**根据用户id查询用户
	 * @param response
	 * @param id 用户id
	 * @throws Exception
	 */
	@RequestMapping("/queryContactsById")
	public void queryContactsById(HttpServletResponse response, String id) throws Exception{
		Contacts contacts = stockOutService.queryContactsById(Long.parseLong(id));
		JSONArray jsonArray = JSONArray.fromObject(contacts);
		if (contacts != null) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}

	/**
	 * 出库时先查询待发货区
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryWaitSendAreaAndStock")
	public void queryWaitSendAreaAndStock(HttpServletResponse response, LzQueryCriteria criteria) throws Exception {
		List<Materiel> materielList = stockOutService.queryWaitSendAreaAndStock(criteria);
		int count = stockOutService.countWaitSendAreaAndStock(criteria);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(materielList);
		jsonObject.put("code", AFFECT_ROW);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 分页查询库存表
	 * @param response
	 * @param criteria
	 */
	@RequestMapping("/queryInventoryInfo")
	public void queryInventoryInfo(HttpServletResponse response, LzQueryCriteria criteria){
		List<ZcInventoryInfoEntity> zcInventoryInfoList = stockOutService.queryInventoryInfo(criteria);
		int count = stockOutService.countInventoryInfo(criteria);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(zcInventoryInfoList);
		jsonObject.put("code", AFFECT_ROW);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询库存信息
	 * @param response
	 * @param zcGeneralQueryEntity
	 */
	@RequestMapping("selectInventoryInfoList")
	public void selectInventoryInfoList(HttpServletResponse response, ZcGeneralQueryEntity zcGeneralQueryEntity){
		if(zcGeneralQueryEntity.getStrWhere() == null){
			zcGeneralQueryEntity.setStrWhere("");
		}
		List<ZcInventoryInfoEntity> inventoryInfoList = zcInventoryManagementService.selectInventoryInfoList(zcGeneralQueryEntity);
		int count = zcInventoryManagementService.countInventoryInfoList(zcGeneralQueryEntity);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(inventoryInfoList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 减库存表
	 * @param materielId
	 * @param materielQuantity
	 */
	@RequestMapping("/inventoryReductionURL")
	public void inventoryReductionURL(HttpServletResponse response, Long materielId, Integer materielQuantity) {
		inventoryReduction(materielId, materielQuantity);
		try {
			ResponseUtil.write(response, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 减物料良品库的库存
	 * @param materielId 物料ID
	 * @param materielQuantity	物料数量
	 */
	public void inventoryReduction(Long materielId, Integer materielQuantity) {
		//根据物料ID查良品库的库存
		List<ZcInventoryInfoEntity> list = cancellingStockDetailService.getInventoryByMid(materielId);
		//遍历减库存（1个物料ID多条库存记录，减后为0的，删除该记录）
		/*for(ZcInventoryInfoEntity info : list){*/
		for(int i = 0; i < list.size(); i++) {
			//System.out.println("补发数量="+materielQuantity+":  库存数量="+info.getmNum());
			materielQuantity = materielQuantity - list.get(i).getSmallNum();
			//如果物料数量大于库存
			if(materielQuantity > 0){
				//删除这条数据
				//cancellingStockDetailService.deleteInventoryById(info.getId());
				int materielQuantity1;
				materielQuantity1= list.get(i).getBigNum() - materielQuantity ;
				if(materielQuantity1 > 0) {
					//修改库存数量
					Map<String, Object> map = new HashMap<>();
					map.put("id", list.get(i).getId());
					map.put("fieldName", "mNum");
					map.put("fieldValue", Math.abs(materielQuantity1));
					cancellingStockDetailService.updateInventory(map);
					Map<String, Object> map1 = new HashMap<>();
					map1.put("id", list.get(i).getId());
					map1.put("fieldName", "bigNum");
					map1.put("fieldValue", Math.abs(materielQuantity1));
					cancellingStockDetailService.updateInventory(map1);
					Map<String, Object> map2 = new HashMap<>();
					map2.put("id", list.get(i).getId());
					map2.put("fieldName", "smallNum");
					map2.put("fieldValue", 0);
					cancellingStockDetailService.updateInventory(map2);
					break;
				} else {
					//删除这条数据
					cancellingStockDetailService.deleteInventoryById(list.get(i).getId());
					//求绝对值
					inventoryReduction(list.get(i).getMid(), Math.abs(materielQuantity1));
				}
			} else {
				//修改库存数量
				Map<String, Object> map = new HashMap<>();
				map.put("id", list.get(i).getId());
				map.put("fieldName", "smallNum");
				map.put("fieldValue", Math.abs(materielQuantity));
				cancellingStockDetailService.updateInventory(map);
				Map<String, Object> map1 = new HashMap<>();
				map1.put("id", list.get(i).getId());
				map1.put("fieldName", "mNum");
				map1.put("fieldValue", Math.abs(materielQuantity)+list.get(i).getBigNum());
				cancellingStockDetailService.updateInventory(map1);
			}
			break;
		}
	}
	
	/**
	 * 查询所有主机厂
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryAllCustomerByType1")
	public void queryAllCustomerByType1(HttpServletResponse response) throws Exception{
		List<Customer> customerList = stockOutService.queryAllCustomerByType1();
		JSONArray jsonArray = JSONArray.fromObject(customerList);
		if (customerList.size() > 0) {
			ResponseUtil.write(response, jsonArray);
		} else {
			ResponseUtil.write(response, null);
		}
	}
	
	/**
	 * 查询该出库单详细是否存在该物料
	 * @param response
	 * @param outStockOrderDetail 出库单详细
	 * @throws Exception
	 */
	@RequestMapping("/queryExistMaterial")
	public void queryExistMaterial(HttpServletResponse response, OutStockOrderDetail outStockOrderDetail) throws Exception{
		List<OutStockOrderDetail> outStockOrderDetailList = stockOutService.queryExistMaterial(outStockOrderDetail);
		if (outStockOrderDetailList.size() > 0) {
			ResponseUtil.write(response, 1);
		} else {
			ResponseUtil.write(response, 0);
		}
	}
}